package com.renwei.yunlong.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.adapter.ServiceSelectProcessingUnitAdapter;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.OwnerCompanyBean;
import com.renwei.yunlong.bean.ServiceCompanyBean;
import com.renwei.yunlong.bean.ServiceLoginBean;
import com.renwei.yunlong.http.HttpPresenter;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.ACache;
import com.zyf.fwms.commonlibrary.http.Api;
import com.zyf.fwms.commonlibrary.utils.AutoUtils;
import com.zyf.fwms.commonlibrary.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 选择服务企业--页面
 * <p>
 * Created by Administrator on 2017/8/31.
 */

public class SelectServiceActivity extends BaseActivity implements View.OnClickListener, HttpTaskListener {

    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.et_search)
    EditText etSearch;
    @Bind(R.id.rl_search)
    RelativeLayout rlSearch;

    private ACache mCache;
    private ServiceSelectProcessingUnitAdapter serviceAdapter;

    private Intent intent;
    ServiceLoginBean serviceLoginBean;
    String companyType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_select_owner);
        ButterKnife.bind(this);

        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        mCache = ACache.get(this);
        //屏幕自适配
        AutoUtils.setSize(this, false, 720, 1280);
        AutoUtils.auto(this);

        initData();
    }

    private void initData() {
        ivBack.setVisibility(View.VISIBLE);
        ivBack.setOnClickListener(this);
        tvTitle.setText("选择服务企业");

        rlSearch.setOnClickListener(this);

        serviceLoginBean = (ServiceLoginBean) mCache.getAsObject("serviceLoginBean");
        companyType = mCache.getAsString("companyType");
        if ("2".equals(companyType)) {//服务商
            if (serviceLoginBean != null) {
                //{"useFlag":1,"type":1,"serviceProviderCode":20170906-100645-74,"currentUserId": 5de5cde1-a1f4-49e9-aa7b-93d7ebf1c54e}
                String jsonBean = "{useFlag:\"" + "1" + "\",type:\"" + "1" + "\",serviceProviderCode:\"" + serviceLoginBean.getRows().getServiceProviderCode() + "\",currentUserId:\"" + serviceLoginBean.getRows().getEmployeeId() + "\",serverName:\"" + "" + "\"}";
                selectServiceHttp(jsonBean);
            }
        }
    }


    //服务商工单处理单位
    private void selectServiceHttp(String jsonBean) {
        HttpPresenter hp = new HttpPresenter(SelectServiceActivity.this);
        hp.setUrl(Api.SERVICE_QUERYSERVERINFO_URL);
        hp.setshowDialogcontent("正在加载");
        hp.setRequsetId(1);
        hp.putParam("jsonBean", jsonBean);
        hp.setCallBack(this);
        hp.post();
    }

    private void setServiceListDate(List<OwnerCompanyBean.Rows> listDate) {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        serviceAdapter = new ServiceSelectProcessingUnitAdapter(SelectServiceActivity.this, listDate);

        serviceAdapter.setOnItemClickListener(new ServiceSelectProcessingUnitAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, String companyName, String bindFlag, String companyCode) {
                intent = new Intent();
                intent.putExtra("companyName", companyName);
                intent.putExtra("bindFlag", bindFlag);
                intent.putExtra("companyCode", companyCode);
                mCache.put("bindFlag",bindFlag);
                mCache.put("companyCode", companyCode);
                setResult(RESULT_OK, intent);
                SelectServiceActivity.this.finish();
            }
        });
        recyclerView.setAdapter(serviceAdapter);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rl_search:
                String search = etSearch.getText().toString().trim();
                if ("2".equals(companyType)) {//服务商
                    if (serviceLoginBean != null) {
                        if (!TextUtils.isEmpty(search)){
                            //{"useFlag":1,"type":1,"serviceProviderCode":20170906-100645-74,"currentUserId": 5de5cde1-a1f4-49e9-aa7b-93d7ebf1c54e}
                            String jsonBean = "{useFlag:\"" + "1" + "\",type:\"" + "1" + "\",serviceProviderCode:\"" + serviceLoginBean.getRows().getServiceProviderCode() + "\",currentUserId:\"" + serviceLoginBean.getRows().getEmployeeId() + "\",serverName:\"" + search + "\"}";
                            selectServiceHttp(jsonBean);
                        }
                    }
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId) {
            case 1:
                OwnerCompanyBean serviceBean = new Gson().fromJson(result, OwnerCompanyBean.class);
                if (serviceBean.getMessage().getCode() == 200) {
                    setServiceListDate(serviceBean.getRows());
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onException(int requestId, String code) {

    }
}
